<?php
/**
 * 
 * 
 */	
class Module_HandlingController extends  Zend_Controller_Action {
	public function init() {
        //Load cau hinh thu muc trong file config.ini
        $app = Zend_Registry::get('App');
        //Lay duong dan thu muc goc (path directory root)
        $this->view->baseUrl = $baseUrl = $this->_request->getBaseUrl() . "/public/";
        $result = array(
                    array($baseUrl.'lib-js/module/Module_Handling.js','js')
					,array($baseUrl.'css/themes/blitzer/jquery-ui-1.8.14.custom.css','css')
					,array($baseUrl.'lib-js/ui/jquery.ui.datepicker-vi.js','js')
                    ,array($baseUrl.'lib-js/tabs.js','js')
                    ,array($baseUrl.'lib-js/system/upload/css/bootstrap.css','css')
                    ,array($baseUrl.'lib-js/system/upload/css/jquery.fileupload.css','css')
                    ,array($baseUrl.'lib-js/system/upload/css/jquery.fileupload-ui.css','css')
                    ,array($baseUrl.'lib-js/system/upload/js/vendor/jquery.ui.widget.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/tmpl.min.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/jquery.iframe-transport.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/jquery.fileupload.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/jquery.fileupload-process.js','js')
                    ,array($baseUrl.'lib-js/system/upload/js/jquery.fileupload-ui.js','js')
                );
        if ($this->_request->isXmlHttpRequest()){
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'partial'
            ));
            $this->view->arrJsCss = Zend_Json::encode($result);
        }else{
            //Cau hinh cho Zend_layout
            Zend_Layout::startMvc(array(
                'layoutPath' =>$app->layout,
                'layout' => 'index'
            ));
            //Load ca thanh phan cau vao trang layout (index.phtml)
            $response = $this->getResponse();
           
            //Dinh nghia current modul code
            $this->view->currentModulCode = $this->_request->getParam('controller');
            $this->view->currentModulCodeForLeft = $this->_request->getParam('action');
            $this->view->postHeader = Clound_String::registerFile($result);
            // echo  Clound_String::registerFile($result); die;
            //Hien thi file template
            $response->insert('menu', $this->view->renderLayout('menu.phtml', './application/layout/scripts/'));     //Hien thi header 
			$response->insert('submenu', $this->view->renderLayout('submenu.phtml', './application/layout/scripts/')); 
            $response->insert('footer', $this->view->renderLayout('footer.phtml', './application/layout/scripts/'));   //Hien thi f
        }
    }
    public function indexAction() {
		
    }
	/**
	*	Lấy dữ liệu cho màn hình danh sách
	*
	*/
    public function dataAction(){
        $objDocSend = new Module_Models_Docsend();
		$session = new Clound_Session();
        $parames = $this->_request->getParams();
		$parames['Status'] = 'DU_THAO';
		$parames['StaffID'] = Zend_Auth::getInstance()->getIdentity()->StaffID;
		$parames['Permistion'] = false;
		$parames['Role'] = 'Y_KIEN_DU_THAO';
		$parames['RoleStatus'] = 'CHO_XU_LY';
        $data = $objDocSend->getAllDocsend($parames);
        echo Zend_Json::encode($data);
        die;
    }
    public function completedAction() {
		
    }
    /**
     * @see : Màn hình xử lý văn bản
     *
     */
    public function processAction() {
		//var_dump($_SESSION);
        if (!$this->_request->isXmlHttpRequest()) {
            return $this->_helper->redirector('index');
        }
		$this->view->addHelperPath('Clound/Xml/Helper/', 'Clound_Xml_Helper');
        $SendID = $this->_request->getParam('SendID');
        //Gan gia tri mac dinh cho cac Input
		$objDate = new Clound_Datetime();
		$sProcessDate = $objDate->ToDdMMyyyy();		
		$model = array(
			'ProcessDate' 	=> $sProcessDate,
			'SendID'		=> $SendID,
			'ProcessType' 	=> '150002'
		);
		$session = new Clound_Session();
		//Lay can bo co quyen ky duyet vb
		$session->getsetStaffsByPermission('160013');		
        // File xml mô tả form
        $filexml = 'xml/module/send/formfield_process.xml';
        $xmlObj = new Clound_Xml($filexml,$model);
        $formObj = $xmlObj->formfieldGenerate();
        $this->view->form=$formObj;
    }
    public function processdataAction() {
		//Lay thong tin qua trinh xu ly
        $SendID = $this->_request->getParam('SendID');
		$modelObj = new Module_Models_WorkSend();
		$model = $modelObj->getAllProcess($SendID);
        echo Zend_Json::encode($model);
        die;
    }
    /**
     * @see : Màn hình xử lý văn bản
     *
     */
    /**
     * @see : Màn hình xử lý văn bản
     *
     */
    public function saveprocessAction() {
        $model = new Module_Models_Docsend();
        //Lay cac tham so cua form
		$iSendID = (int)$this->_request->getParam('SendID');
		$objDate = new Clound_Datetime();
		$sProcessDate = $objDate->convertDateTime($this->_request->getParam('ProcessDate'));
		$sResult = trim($this->_request->getParam('Result'));
		$sProcessType = $this->_request->getParam('ProcessType');
		$sProcessType = $sProcessType[0];
		//Update tien do
		$modelWork = new Module_Models_WorkSend();
		//Mang gia tri
		$iDepartmentDraf = Zend_Auth::getInstance()->getIdentity()->UnitID;
		$iStaffDraf = Zend_Auth::getInstance()->getIdentity()->StaffID;
		$sPositionName = Zend_Auth::getInstance()->getIdentity()->Name;		
		$data = array(
			"WorkID"			=> 	0,
			"DocID"				=> 	$iSendID,
			"StaffID"			=> 	$iStaffDraf,
			"PositionName"		=> 	$sPositionName,
			"DepartmentID"		=> 	$iDepartmentDraf,
			"WorkType"			=> 	'150002',//150002
			"WorkDate"			=> 	$sProcessDate,
			"Result"			=> 	$sResult
		);
		$errors = array();
		$errors = $modelWork->insert($data);
		$iWorkID = $modelWork->getAdapter()->lastInsertId();
		//Update file
		$fileObj = new Clound_File(null,false);
		$modelFile = new System_Models_File();
		$sFileIdList = '';
		$sFileNameList = '';
		$sAttachfile = $this->_request->getParam('attachfile');
		$arrFileNameList = array();
		if ($sAttachfile != '') {
			$arrFileNameList = $fileObj->_uploadFileAttachList($sAttachfile);
		}
		$count = sizeof($arrFileNameList);
		if($count){
			for ($i = 0; $i < $count; $i++) {
				$sFileId = '';
				//Mang gia tri
				$data = array(
					"FileID"			=> 	0,
					"RecordID"			=> 	$iWorkID,
					"RecordType"		=> 	'WORK',
					"FileType"			=> 	strtolower(pathinfo($arrFileNameList[$i], PATHINFO_EXTENSION)),
					"FileName"			=> 	$arrFileNameList[$i],
					"TableName"			=> 	'worksend'
				);
				$modelFile->insert($data);
				$sFileId = $modelFile->getAdapter()->lastInsertId();
				if($sFileIdList==''){
					$sFileIdList = $sFileId;
					$sFileNameList = $arrFileNameList[$i];
				}else{
					$sFileIdList .= ','.$sFileId;
					$sFileNameList .= '!#!'.$arrFileNameList[$i];
				}
			}
		}	
		$data = array(
					"WorkID"		=> 	$iWorkID,
					"FileID"		=> 	$sFileIdList,
					"FileName"		=> 	$sFileNameList
				);
		$where="WorkID ='$iWorkID'";
		$modelWork->update($data,$where);
        echo Zend_Json::encode($errors);
        die;
    }
}?>
